// -*- coding: utf-8 -*-
frappe.ui.form.on('Scrap Receipt Item', {
    scrap_material: function(frm, cdt, cdn) {
        let row = locals[cdt][cdn];
        
        // 当选择余料时，自动填充余料信息
        if (row.scrap_material) {
            frappe.call({
                method: 'frappe.client.get',
                args: {
                    doctype: 'Scrap Material',
                    name: row.scrap_material
                },
                callback: function(r) {
                    if (r.message) {
                        let material = r.message;
                        frappe.model.set_value(cdt, cdn, 'material_type', material.material_type);
                        frappe.model.set_value(cdt, cdn, 'material_name', material.material_name);
                        frappe.model.set_value(cdt, cdn, 'color_pattern', material.color_pattern);
                        frappe.model.set_value(cdt, cdn, 'length', material.length);
                        frappe.model.set_value(cdt, cdn, 'width', material.width);
                        frappe.model.set_value(cdt, cdn, 'thickness', material.thickness);
                        frappe.model.set_value(cdt, cdn, 'unit', material.unit);
                        frappe.model.set_value(cdt, cdn, 'quantity', material.quantity);
                    }
                }
            });
        }
    },
    
    location: function(frm, cdt, cdn) {
        let row = locals[cdt][cdn];
        
        // 当选择库位时，验证库位是否属于入库单的仓库
        if (row.location && frm.doc.warehouse) {
            frappe.call({
                method: 'frappe.client.get_value',
                args: {
                    doctype: 'Scrap Location',
                    filters: {name: row.location},
                    fieldname: ['warehouse', 'status']
                },
                callback: function(r) {
                    if (r.message) {
                        if (r.message.warehouse !== frm.doc.warehouse) {
                            frappe.msgprint(__('所选库位不属于当前仓库，请重新选择'));
                            frappe.model.set_value(cdt, cdn, 'location', '');
                        } else if (r.message.status !== '启用') {
                            frappe.msgprint(__('所选库位未启用，请选择其他库位'));
                            frappe.model.set_value(cdt, cdn, 'location', '');
                        }
                    }
                }
            });
        }
    },
    
    quantity: function(frm, cdt, cdn) {
        let row = locals[cdt][cdn];
        
        // 验证数量
        if (row.quantity && row.quantity <= 0) {
            frappe.msgprint(__('数量必须大于0'));
            frappe.model.set_value(cdt, cdn, 'quantity', 1);
        }
        
        // 检查余料数量是否足够
        if (row.quantity && row.scrap_material) {
            frappe.call({
                method: 'frappe.client.get_value',
                args: {
                    doctype: 'Scrap Material',
                    filters: {name: row.scrap_material},
                    fieldname: 'quantity'
                },
                callback: function(r) {
                    if (r.message && row.quantity > r.message.quantity) {
                        frappe.msgprint(__('余料数量不足，当前库存: {0}，入库数量: {1}', [r.message.quantity, row.quantity]));
                        frappe.model.set_value(cdt, cdn, 'quantity', r.message.quantity);
                    }
                }
            });
        }
    }
});

// 库位过滤器函数
function get_location_filter_for_receipt(frm) {
    return {
        filters: {
            warehouse: frm.doc.warehouse,
            status: '启用'
        }
    };
}